//https://leetcode.cn/problems/last-stone-weight-ii/
package codeRandomThoughts.Test1049最后一块石头的重量II;

import java.util.Arrays;

/**
 * 也是01背包问题
 */
public class Solution {
    public int lastStoneWeightII(int[] stones) {
        int sum=0;
        for (int stone : stones) {
            sum+=stone;
        }
        int target = sum >> 1;

        int[] dp = new int[target + 1];

        for (int i = 0; i < stones.length; i++) {
            for (int j = target; j >= stones[i]; j--) {
                dp[j] = Math.max(dp[j], dp[j - stones[i]] + stones[i]);
            }
        }

        return sum - 2 * dp[target];
    }
}
